import numpy as np
import matplotlib.pyplot as plt
import pickle
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from tqdm import tqdm

# Step 1: 加载手写数字数据集
digits = load_digits()
X = digits.data
y = digits.target

# Step 2: 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化变量
k_range = range(1, 41)
accuracies = []
best_k = 0
best_accuracy = 0.0
best_knn_model = None

# Step 3: 尝试不同的K值，并记录准确率
for k in tqdm(k_range, desc="Finding the best k"):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)

    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k
        best_knn_model = knn

# Step 4: 保存最优的KNN模型到pickle文件中
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_knn_model, f)

# Step 5: 绘制准确率变化图，并保存为PDF
plt.figure(figsize=(10, 6))
plt.plot(k_range, accuracies, marker='o', linestyle='-', color='blue')
plt.axvline(x=best_k, color='red', linestyle='--')
plt.text(best_k, best_accuracy, f'k={best_k}, Accuracy={best_accuracy:.4f}', 
         horizontalalignment='right', verticalalignment='bottom')
plt.title('K-Value vs. Accuracy')
plt.xlabel('K Value')
plt.ylabel('Accuracy')
plt.grid(True)
plt.savefig('accuracy_plot.pdf')

# 打印最优的K值和对应的准确率
print(f"Best K: {best_k}, Best Accuracy: {best_accuracy:.4f}")